/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vista;

import Logica.Acoplador;
import Logica.RequestParameter;
import gestorSimilar.Tema;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Jah
 */
public class similar extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String html = getHtml(request);

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Similar</title>");
            out.println("<link href=\"style.css\" rel=\"stylesheet\" type=\"text/css\" />");
            out.println("</head>");
            out.println("<body>");
            out.println(html);
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    private String getHtml(HttpServletRequest request) {
        String res = "Hubo algún error", accion;
        Acoplador a = (Acoplador) request.getSession().getAttribute("acoplador");
        if (a == null) {
            a = new Acoplador();
            request.getSession().setAttribute("acoplador", a);
        }

        accion = request.getParameter("submit");

        if (accion == null) {
            return a.getFormLogin();
        } else {
            if (accion.equalsIgnoreCase("ENTRAR")) {
                String email = request.getParameter("user");
                String pass = request.getParameter("pass");
                res = a.autenticaUsuario(email, pass);
            } else if (accion.equalsIgnoreCase("REGISTRO")) {
                res = a.getFormRegistro();
            } else if (accion.equalsIgnoreCase("MODERAR")) {
                res = a.getFormTodasPaginas();
            } else if (accion.equalsIgnoreCase("REGISTRARME")) {
                // Registrar al usuario. 
                // Los datos que se envian son: 
                //      Nombre, ap paterno, ap materno, email, contra, temas
                // Se debe verificar que la contrasena sea segura
                HashMap<String, String> datos = new HashMap<String, String>();
                HashMap<String, String> temas = new HashMap<String, String>();

                datos.put("userNom", request.getParameter("userNom"));
                datos.put("userApp", request.getParameter("userApp"));
                datos.put("userApm", request.getParameter("userApm"));
                datos.put("email", request.getParameter("email"));
                datos.put("pass", request.getParameter("pass"));

                String[] strTemas = request.getParameterValues("temas");
                String[] parseStr;
                if (strTemas != null) {
                    for (String string : strTemas) {
                        parseStr = string.split(",");
                        temas.put(parseStr[0], parseStr[1]);
                    }
                }else{
                    temas=null;
                }
                res = a.registroUsuario(datos, temas);

            } else if (accion.equalsIgnoreCase("VER DETALLE DE PAGINA")) {
                String url = request.getParameter("urlPagina");
                res = a.getPagina(url);
                // Se debe mostrar al moderador.

            } else if (accion.equalsIgnoreCase("CAMBIAR DETALLES DE PAGINA")) {
                //
            } else if (accion.equalsIgnoreCase("ELIMINAR")) {
                String url = request.getParameter("urlPagina");
                res = a.eliminaPagina(url);

            } else if (accion.equalsIgnoreCase("SIGUIENTE")) {
                res=a.getSiguientePagina();
            } else if (accion.equalsIgnoreCase("UPVOTE")) {
                //res=a.getSiguientePagina();
            } else if (accion.equalsIgnoreCase("DOWNVOTE")) {
                //res=a.getSiguientePagina();
            }
            
        }


        return res;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
